package handler

import (
	"fitness/go-admin/internal/model"
	"fitness/go-admin/internal/service"
	"fitness/go-admin/pkg/response"
	"strconv"

	"github.com/gin-gonic/gin"
)

// QuickReplyHandler 快捷回复处理器
type QuickReplyHandler struct {
	service service.QuickReplyService
}

// NewQuickReplyHandler 创建快捷回复处理器
func NewQuickReplyHandler(service service.QuickReplyService) *QuickReplyHandler {
	return &QuickReplyHandler{
		service: service,
	}
}

// CreateQuickReply 创建快捷回复
// @Summary 创建快捷回复
// @Description 创建常用话术
// @Tags 客服系统-快捷回复
// @Accept json
// @Produce json
// @Security Bearer
// @Param request body model.CreateQuickReplyReq true "创建请求"
// @Success 200 {object} response.Response{data=model.ChatQuickReply}
// @Router /api/v1/chat/quick-replies [post]
func (h *QuickReplyHandler) CreateQuickReply(c *gin.Context) {
	userID := c.GetUint("userID")

	var req model.CreateQuickReplyReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, 400, "参数错误")
		return
	}

	reply, err := h.service.CreateQuickReply(userID, &req)
	if err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, reply, "创建成功")
}

// GetQuickReply 获取快捷回复详情
// @Summary 获取快捷回复详情
// @Description 获取单个快捷回复的详细信息
// @Tags 客服系统-快捷回复
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "快捷回复ID"
// @Success 200 {object} response.Response{data=model.ChatQuickReply}
// @Router /api/v1/chat/quick-replies/{id} [get]
func (h *QuickReplyHandler) GetQuickReply(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.Error(c, 400, "ID格式错误")
		return
	}

	reply, err := h.service.GetQuickReply(uint(id))
	if err != nil {
		response.Error(c, 404, err.Error())
		return
	}

	response.Success(c, reply, "")
}

// GetQuickReplyList 获取快捷回复列表
// @Summary 获取快捷回复列表
// @Description 获取当前客服的快捷回复列表（包括个人话术和全局话术）
// @Tags 客服系统-快捷回复
// @Accept json
// @Produce json
// @Security Bearer
// @Param category query string false "分类筛选"
// @Param isGlobal query bool false "是否只看全局话术"
// @Param page query int false "页码"
// @Param pageSize query int false "每页数量"
// @Success 200 {object} response.Response{data=model.QuickReplyListResp}
// @Router /api/v1/chat/quick-replies [get]
func (h *QuickReplyHandler) GetQuickReplyList(c *gin.Context) {
	userID := c.GetUint("userID")

	var req model.QuickReplyListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, 400, "参数错误")
		return
	}

	result, err := h.service.GetQuickReplyList(userID, &req)
	if err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, result, "")
}

// UpdateQuickReply 更新快捷回复
// @Summary 更新快捷回复
// @Description 更新快捷回复内容
// @Tags 客服系统-快捷回复
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "快捷回复ID"
// @Param request body model.UpdateQuickReplyReq true "更新请求"
// @Success 200 {object} response.Response
// @Router /api/v1/chat/quick-replies/{id} [put]
func (h *QuickReplyHandler) UpdateQuickReply(c *gin.Context) {
	userID := c.GetUint("userID")

	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.Error(c, 400, "ID格式错误")
		return
	}

	var req model.UpdateQuickReplyReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, 400, "参数错误")
		return
	}

	if err := h.service.UpdateQuickReply(uint(id), userID, &req); err != nil {
		response.Error(c, 400, err.Error())
		return
	}

	response.Success(c, nil, "更新成功")
}

// DeleteQuickReply 删除快捷回复
// @Summary 删除快捷回复
// @Description 删除快捷回复
// @Tags 客服系统-快捷回复
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "快捷回复ID"
// @Success 200 {object} response.Response
// @Router /api/v1/chat/quick-replies/{id} [delete]
func (h *QuickReplyHandler) DeleteQuickReply(c *gin.Context) {
	userID := c.GetUint("userID")

	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.Error(c, 400, "ID格式错误")
		return
	}

	if err := h.service.DeleteQuickReply(uint(id), userID); err != nil {
		response.Error(c, 400, err.Error())
		return
	}

	response.Success(c, nil, "删除成功")
}

